package com.my.study.algorithm.leetcode;

import java.util.Stack;

/**
 * <p>
 *     定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
 * </p>
 * 
 * @author Carlos
 * @version 1.0.0
 * @date 2023/1/2 16:37
 */
public class MinStack {

    private Stack<Integer> mainStack;
    private Stack<Integer> stack;
    
    /** initialize your data structure here. */
    public MinStack() {
        mainStack = new Stack<>();
        stack = new Stack<>();
    }

    public void push(int x) {
        mainStack.push(x);
        if (stack.isEmpty() || x <= stack.peek()) {
            stack.push(x);
        }
    }

    public void pop() {
        if (mainStack.pop().equals(stack.peek())) {
            stack.pop();
        }
    }

    public int top() {
        return mainStack.peek();
    }

    public int min() {
        return stack.peek();
    }

}
